﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ControleRepresentante.Common
{
    public static class BatchExtensionHelper
    {
        public static void ExecuteBatch<T>(this IEnumerable<T> values, int count, Action<IEnumerable<T>> action)
        {
            List<T> buffer = new List<T>();

            foreach (T value in values)
            {
                buffer.Add(value);
                if (buffer.Count == count)
                {
                    action(buffer);
                    buffer.Clear();
                }
            }

            if (buffer.Any())
            {
                action(buffer);
            }
        }
    }
}
